<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge"/>
    <title>JSBB - Vue前后端不分离</title>
    <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport"/>
    <link rel="icon" th:href="@{/static/favicon.ico}">
    <link rel="stylesheet" th:href="@{/static/element/index.css}">
</head>
<body>
<div id="app">
    <template>
        <div class="login-page">
            <el-form :model="loginForm" :rules="rules" ref="loginForm" label-position="left" label-width="0px">
                <h3 class="title">系统登录</h3>
                <el-form-item prop="userName">
                    <el-input type="text" v-model="loginForm.userName" placeholder="用户名"></el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input type="password" v-model="loginForm.password" placeholder="密码"></el-input>
                </el-form-item>
                <el-form-item style="width:100%;">
                    <el-button type="primary" style="width:100%;" @click="login">登录</el-button>
                </el-form-item>
            </el-form>
        </div>
    </template>
</div>
</body>
<script th:src="@{/static/js/axios.min.js}"></script>
<script th:src="@{/static/js/common.js}"></script>
<script th:src="@{/static/js/vue.min.js}"></script>
<script th:src="@{/static/element/index.js}"></script>
<script>
new Vue({
    el: '#app',
    data() {
        return {
            loginForm: {
                userName: 'admin',
                password: '1',
            },
            rules: {
                userName: [{required: true, message: '请输入用户名', trigger: 'blur'}],
                password: [{required: true, message: '请输入密码', trigger: 'blur'}]
            },
            checked: false,
            buttons: [],
        }
    },
	beforeCreate: function() {
		if (self != top) {
			top.location.href = self.location.href;
		}
	},
    methods: {
        login(event) {
            this.$refs.loginForm.validate((valid) => {
                if (valid) {
                    service.post('/login', {
                        userName:this.loginForm.userName,
                        pwd:this.loginForm.password
                    })
                    .then((res) => {
                        if (res.code == '0000') {
                            localStorage.setItem('jsbb-token', JSON.stringify(res.data.token));
                            localStorage.setItem('jsbb-sysUser', JSON.stringify(res.data.sysUser));
                            localStorage.setItem('jsbb-sysMenu', JSON.stringify(res.data.sysMenu));
                            this.parseTreeJson(res.data.sysMenu);
                            localStorage.setItem('jsbb-sysButtons', JSON.stringify(this.buttons));
                            parent.location.href ='';
                        } else {
                            this.$message.error(res.message);
                        }
                    });
                } else {
                    return false;
                }
            })
        },
        parseTreeJson(treeNodes) {
            for(let i = 0; i < treeNodes.length; i ++) {
                if (treeNodes[i].url) {
                    this.buttons.push(treeNodes[i].url);
                }
                let child = treeNodes[i].children;
                if(child && child.length > 0) {
                     this.parseTreeJson(child);
                }
            }
        }
    }
})
</script>
<style>
.login-page {
    top: 40%;
    left: 50%;
    position: absolute;
    transform: translate(-50%,-50%);
    overflow: hidden;
    width: 350px;
    border-radius: 5px;
    padding: 35px 35px 15px;
    background: #fff;
    border: 1px solid #eaeaea;
    box-shadow: 0 0 25px #cac6c6;
}
</style>
</html>
